package com.freesky.biz.icbc.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.fastjson2.JSON;
import com.freesky.biz.icbc.domain.TMarketingActivityData;
import com.freesky.biz.icbc.domain.TPartnership;
import com.freesky.biz.icbc.excel.TPartnershipImport;
import com.freesky.biz.icbc.mapper.MarketingActivityDataMapper;
import com.freesky.biz.icbc.mapper.PartnershipMapper;
import com.freesky.biz.icbc.service.IMarketingActivityDataService;
import com.freesky.biz.icbc.service.IPartnershipService;
import com.freesky.biz.utils.SeqUtil;
import com.freesky.common.constant.HttpStatus;
import com.freesky.common.core.domain.AjaxResult;
import com.freesky.common.core.domain.entity.SysUser;
import com.freesky.common.enums.ExportStatusEnum;
import com.freesky.common.exception.CustomException;
import com.freesky.common.utils.DateUtils;
import com.freesky.common.utils.SecurityUtils;
import com.freesky.common.utils.ServletUtils;
import com.freesky.common.utils.StringUtils;
import com.freesky.common.utils.file.FileDownloadUtils;
import com.freesky.common.utils.poi.ExcelUtil;
import com.freesky.system.domain.TExportRecord;
import com.freesky.system.domain.TImportRecord;
import com.freesky.system.service.ITExportRecordService;
import com.freesky.system.service.ITImportRecordService;
import com.freesky.system.task.SyncReport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;

@Service
public class MarketingActivityDataServiceImpl implements IMarketingActivityDataService {

    private static final Logger logger = LoggerFactory.getLogger(MarketingActivityDataServiceImpl.class);

    @Autowired
    private MarketingActivityDataMapper activityDataMapper;

    @Autowired
    private ITExportRecordService exportRecordService;

    @Autowired
    private SyncReport syncReport;

    @Override
    public List<TMarketingActivityData> findList(TMarketingActivityData entity) {
        return activityDataMapper.findList(entity);
    }

    @Override
    public AjaxResult exportFile(TMarketingActivityData entity) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        TExportRecord exportRecord = new TExportRecord();
        exportRecord.setId(SeqUtil.getSeqId());
        exportRecord.setBizType("MARKETING_DATA");
        exportRecord.setCreateBy(String.valueOf(sysUser.getUserId()));

        TExportRecord exportRecordSelect = exportRecordService.selectRecordExportByStatus(exportRecord);
        if (exportRecordSelect != null) {
            return AjaxResult.error("已经有下载任务");
        }

        exportRecord.setStatus(ExportStatusEnum.SUBMIT_WAIT.getValue());
        System.out.println(JSON.toJSONString(exportRecord));
        exportRecordService.insert(exportRecord);

        syncReport.exportFileMutilHeader(entity, new TMarketingActivityData(), IMarketingActivityDataService.class,
                "findList", "营销日历数据",Long.parseLong(exportRecord.getId()),sysUser.getUserName());

        return AjaxResult.success();
    }
}
